package edu.its.dreamteam.web.pages.manager;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.tapestry.ComponentResources;
import org.apache.tapestry.annotations.Retain;
import org.apache.tapestry.beaneditor.BeanModel;
import org.apache.tapestry.ioc.annotations.Inject;
import org.apache.tapestry.services.BeanModelSource;

import edu.its.dreamteam.daf.beans.Club;
import edu.its.dreamteam.daf.beans.DraftPlayer;
import edu.its.dreamteam.daf.beans.Player;
import edu.its.dreamteam.daf.beans.Team;
import edu.its.dreamteam.daf.dao.IClubDao;
import edu.its.dreamteam.daf.dao.IDraftPlayerDao;
import edu.its.dreamteam.daf.dao.IPlayerDao;
import edu.its.dreamteam.daf.dao.ITeamDao;
import edu.its.dreamteam.web.base.SecureBasePage;
import edu.its.dreamteam.web.pages.entity.ClubPlayer;

public class BuyPlayerPage extends SecureBasePage{
	@Inject
	private IPlayerDao playerDao;
	@Inject
	private IClubDao clubDao;
	@Inject
	private ITeamDao teamDao;
	
	@Inject
	private IDraftPlayerDao draftPlayerDao;
	
	
	private List<Player> playerList;		
	private List<Club> clubList;
	private List<ClubPlayer> clubPlayerList;
	private List<DraftPlayer> draftPlayerList;
	private List<DraftPlayer> draftPlayerBidList;	
	
	private Player player;	
	private Club club;	
	private ClubPlayer clubPlayer;
	private DraftPlayer draftPlayer;
	private Team team;
	
	@Retain
	private BeanModel playerModel;
	@Retain
	private BeanModel draftPlayerModel;
	@Retain
	private BeanModel draftPlayerBidModel;
	@Inject
	private BeanModelSource beanModelSource;
	@Inject
	private ComponentResources resources;
		

	private int bidPlayer;
    
    
	public Player getPlayer() {
		return player;
	}
	public void setPlayer(Player player) {
		this.player = player;
	}		
	public Club getClub() {
		return club;
	}
	public void setClub(Club club) {
		this.club = club;
	}		
	public ClubPlayer getClubPlayer() {
		return clubPlayer;
	}
	public void setClubPlayer(ClubPlayer clubPlayer) {
		this.clubPlayer = clubPlayer;
	}	
	
	public DraftPlayer getDraftPlayer() {
		return draftPlayer;
	}
	public void setDraftPlayer(DraftPlayer draftPlayer) {
		this.draftPlayer = draftPlayer;
	}	
	
	public Team getTeam() {
		if(team==null){
			team = (Team)getTeamDao().queryForBaseBean("\"username\" ='"+getVisit().getUsername()+"' ");
		}
		return team;
	}
	public void setTeam(Team team) {
		this.team = team;
	}
	
	
	public IPlayerDao getPlayerDao() {
		return playerDao;
	}			
	public IClubDao getClubDao() {
		return clubDao;
	}	
	public IDraftPlayerDao getDraftPlayerDao() {
		return draftPlayerDao;
	}	
	public ITeamDao getTeamDao() {
		return teamDao;
	}
	
	public int getBidPlayer() {
		return bidPlayer;
	}
	public void setBidPlayer(int bidPlayer) {
		this.bidPlayer = bidPlayer;
	}
	
	public BeanModel getPlayerModel() {
		return playerModel;
	}
	public BeanModel getDraftPlayerModel() {
		return draftPlayerModel;
	}
	public BeanModel getDraftPlayerBidModel() {
		return draftPlayerBidModel;
	}
	
	public List<Player> getPlayerList() {
		if(playerList == null){
			playerList = new ArrayList<Player>();
			playerList = getPlayerDao().queryForList("", "");
		}
		return playerList;
	}		
	public void setPlayerList(List<Player> playerList) {
		this.playerList = playerList;
	}
		
	public List<Club> getClubList() {
		if(clubList == null){
			clubList = new ArrayList<Club>();
			clubList = getClubDao().queryForList("", "");
		}
		return clubList;
	}
	public void setClubList(List<Club> clubList) {
		this.clubList = clubList;
	}	
	
	public List<ClubPlayer> getClubPlayerList() {		
		if(clubPlayerList==null){
			clubPlayerList = new ArrayList<ClubPlayer>();
			System.out.println(getTeam().getIdTeam());
			List<DraftPlayer> draftPlayer = getDraftPlayerDao().queryForList("\"id_team\" ='"+getTeam().getIdTeam()+"'", "");
			
			if(draftPlayer!=null && draftPlayer.size()!=0){
				for(Club club : getClubList()){
					List<Player> playerByClubList = getPlayerDao().getPlayerByRoom(getVisit().getUsername(), getTeam().getIdTeam(), null, club.getIdClub(),getTeam().getIdTeam());
					if(playerByClubList.size()!=0){
						ClubPlayer clubPlayer = new ClubPlayer(club,playerByClubList);					
						clubPlayerList.add(clubPlayer);
					}				
				}
				
			}else{
				for(Club club : getClubList()){
					List<Player> playerByClubList = getPlayerDao().queryForList("\"id_club\" ='"+club.getIdClub()+"'", "");
					if(playerByClubList.size()!=0){
						ClubPlayer clubPlayer = new ClubPlayer(club,playerByClubList);					
						clubPlayerList.add(clubPlayer);
					}				
				}
			}			
		}
		return clubPlayerList;
	}
	public void setClubPlayerList(List<ClubPlayer> clubPlayerList) {
		this.clubPlayerList = clubPlayerList;
	}
	
	
	public List<DraftPlayer> getDraftPlayerList() {
		if(draftPlayerList==null){
			draftPlayerList = new ArrayList<DraftPlayer>();
			draftPlayerList = getDraftPlayerDao().queryForList("\"id_team\"='"+getTeam().getIdTeam()+"' AND \"status_draft\"='"+"0"+"'", "");
		}
		return draftPlayerList;
	}
	
	public void setDraftPlayerList(List<DraftPlayer> draftPlayerList) {
		this.draftPlayerList = draftPlayerList;
	}
	
	
	public List<DraftPlayer> getDraftPlayerBidList() {
		if(draftPlayerBidList==null){
			draftPlayerBidList = getDraftPlayerDao().getDraftPlayerByRoomList(getTeam().getIdRoom(), "0",null);
		}
		return draftPlayerBidList;
	}
	public void setDraftPlayerBidList(List<DraftPlayer> draftPlayerBidList) {
		this.draftPlayerBidList = draftPlayerBidList;
	}
		
		
	void pageLoaded(){
		playerModel = beanModelSource.create(Player.class, false, resources);
		playerModel.get("idClub").label("Club");
		playerModel.get("idPosition").label("Position");
		playerModel.add("actions", null).label("Buy");
		
		draftPlayerModel = beanModelSource.create(DraftPlayer.class, false, resources);
		draftPlayerModel.remove("idDraft","idTeam","statusDraft");
		draftPlayerModel.get("idPlayer").label("Player");
		draftPlayerModel.add("actions", null).label("Cancel");
		
		draftPlayerBidModel = beanModelSource.create(DraftPlayer.class, false, resources);
		draftPlayerBidModel.remove("idDraft","statusDraft");
		draftPlayerBidModel.get("idPlayer").label("Player");
		
	}
	void onSuccessFromClubPlayerForm(String idPlayer){	
		
		DraftPlayer draftPlayerByRoom = getDraftPlayerDao().getDraftPlayerByRoom(idPlayer, getTeam().getIdRoom(), "0");
		List<DraftPlayer> draftPlayerManager = getDraftPlayerDao().queryForList("\"id_team\"='"+getTeam().getIdTeam()+"'", null);
		Player playerManager = (Player)getPlayerDao().findByPrimaryKey(idPlayer);
		
		if(draftPlayerManager.size()<=15){
			if(getBidPlayer()>playerManager.getDefaultPrice()){
				if(draftPlayerByRoom!=null){
					if(getBidPlayer() > draftPlayerByRoom.getBid()){
						draftPlayerByRoom.setIdTeam(getTeam().getIdTeam());
						draftPlayerByRoom.setBid(getBidPlayer());
						draftPlayerByRoom.setDateDraft(new Date());
						draftPlayerByRoom.setStatusDraft("0");
						draftPlayerByRoom.setMaxPlayed(3);
						draftPlayerByRoom.setCurrentPlayed(0);
						draftPlayerByRoom.setPlay("0");
						draftPlayerByRoom.setSell("0");
						getDraftPlayerDao().update(draftPlayerByRoom);
						getTeam().setPoint(getTeam().getPoint()-getBidPlayer());
						getTeamDao().update(getTeam());
					}else{
						//harga penawaran kurang tinggi
					}
				}else{
					DraftPlayer draftPlayerRoomObj = new DraftPlayer();
					draftPlayerRoomObj.setIdPlayer(idPlayer);
					draftPlayerRoomObj.setIdTeam(getTeam().getIdTeam());
					draftPlayerRoomObj.setBid(getBidPlayer());
					draftPlayerRoomObj.setDateDraft(new Date());
					draftPlayerRoomObj.setStatusDraft("0");
					draftPlayerRoomObj.setMaxPlayed(3);
					draftPlayerRoomObj.setCurrentPlayed(0);
					draftPlayerRoomObj.setPlay("0");
					draftPlayerRoomObj.setSell("0");
					getDraftPlayerDao().insert(draftPlayerRoomObj);
					getTeam().setPoint(getTeam().getPoint()-getBidPlayer());
					getTeamDao().update(getTeam());
				}
			}else{
				// harga di bawah harga standart
			}			
			
		}else{
			//maksimum kapacity telah terpenuhi
		}
	}
	void onSuccessFromDraftPlayerForm(String idDraft){
		DraftPlayer draftPlayer = (DraftPlayer)getDraftPlayerDao().findByPrimaryKey(idDraft);
		getDraftPlayerDao().delete(draftPlayer);
		getTeam().setPoint(getTeam().getPoint() + draftPlayer.getBid());
		getTeamDao().update(getTeam());
	}
		
	
}
